package com.hongruan.action.Menu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.hongruan.action.BaseAction;
import com.hongruan.bean.AccountAuthority;
import com.hongruan.bean.Menu;
import com.hongruan.model.ResultStatusModel;
import com.hongruan.service.AccountAuthorityService;
import com.hongruan.service.MenuService;
import com.hongruan.utils.DBDataLoader;

/**
 * 菜单相关操作
 * 
 * @author ZhangWenLong
 * 
 */
@Controller("MenuAction")
@Scope("prototype")
public class MenuAction extends BaseAction {

	private static final long serialVersionUID = -721913847689568627L;

	private static Logger logger = Logger.getLogger(MenuAction.class);

	@Resource
	private MenuService menuService;

	@Resource
	private AccountAuthorityService accountAuthorityService;
	
	private Menu menu;

	public String menuList() {
		logger.info("menuList() begin");
		
        JSONObject json = new JSONObject();
        ResultStatusModel result = new ResultStatusModel();
        
        try{
        	
            List<Menu> menuList = DBDataLoader.getMenuList();
            if(CollectionUtils.isEmpty(menuList)){
            	menuList = this.menuService.getMenuList(menu.getMenuLevel());
            }
            
            //复制一份菜单供当前用户使用
            List<Menu> accountMenuList = new ArrayList<Menu>();
            
            //获取当前用户的菜单权限
            AccountAuthority accountAuthority = new AccountAuthority();
            accountAuthority.setAccountId(this.getCurrentAccount().getId());
            List<AccountAuthority> accountAuthorityList = this.accountAuthorityService.read(accountAuthority);
            
            if(CollectionUtils.isNotEmpty(accountAuthorityList)){
            	//list转Map，通过get方法判断是否有权限现实菜单
            	Map<Long,Long> authorityMap = new HashMap<Long,Long>();
            	for(AccountAuthority aAuthority:accountAuthorityList){
            		authorityMap.put(aAuthority.getMenuId(), aAuthority.getMenuId());
            	}
            	
        		//根目录
        		for(Menu menu:menuList){
        			if(authorityMap.get(menu.getId())!=null||authorityMap.get(0L)!=null){
        				accountMenuList.add(menu);
        				
        				//二级目录
        				List<Menu> mList = new ArrayList<Menu>();
        				for(Menu secondMenu:menu.getChildMenu()){
                			if((authorityMap.get(secondMenu.getId())!=null || authorityMap.get(0L)!=null) 
                					&& secondMenu.getMenuType().equals(menu.getMenuType())){
                				mList.add(secondMenu);
                				
                				//三级目录
                				List<Menu> childList = new ArrayList<Menu>();
                				for(Menu childMenu:secondMenu.getChildMenu()){
                        			if(authorityMap.get(childMenu.getId())!=null||authorityMap.get(0L)!=null){
                        				childList.add(childMenu);
                        			}
                				}
                				secondMenu.setChildMenu(childList);
                			}
        				}
        				menu.setChildMenu(mList);
        			}
        		}
            }else{
            	accountMenuList = menuList;
            }
            
            json.put("menuList", accountMenuList);
            
            result.setSuccess(true);
            
        }catch(Exception e){
            logger.error("menuList()... error",e);
            
            result.setSuccess(false);
        }
        
        json.put("result", result);
        
        this.writeAjaxResult(json.toString());
        
        return null;
	}
	
	public String getMenuList(){
		logger.info("getListMenu() begin");
		
        JSONObject json = new JSONObject();
        ResultStatusModel result = new ResultStatusModel();
        
        try{
        	List list = this.menuService.getMenuListById(menu.getId());
        	if(list.size() > 0){
        		json.put("list", list);
            	result.setSuccess(true);
        	}else{
        		result.setSuccess(false);
        	}
        }catch(Exception e){
        	e.printStackTrace();
        	result.setSuccess(false);
        }
        json.put("result", result);
        this.writeAjaxResult(json.toString());
		return null;
	}

	public Menu getMenu() {
		return menu;
	}

	public void setMenu(Menu menu) {
		this.menu = menu;
	}
	
}
